
package com.idealighter.game.dictionary.dic;

import com.google.inject.Inject;
import com.google.inject.Singleton;

import com.idealighter.game.dictionary.core.DictionaryTable;
import com.idealighter.game.dictionary.domain.ShuihuRoomDomain;
import com.idealighter.game.dictionary.mapper.ShuihuRoomMapper;
import com.idealighter.utils.check.EmptyUtil;
import com.idealighter.utils.json.JsonUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Singleton
public class ShuihuRoomDic implements DictionaryTable<ShuihuRoomDomain> {

  private ShuihuRoomMapper mapper;
  private List<ShuihuRoomDomain> list;
  private Map<Integer, ShuihuRoomDomain> map;

  @Inject
  public ShuihuRoomDic(ShuihuRoomMapper mapper) {
    this.mapper = mapper;
    load();
  }

  @Override
  public List<ShuihuRoomDomain> list() {

    return list;
  }

  @Override
  public Map<Integer, ShuihuRoomDomain> map() {

    return map;
  }

  @Override
  public void load() {
    this.list = mapper.selectAll();
    Map<Integer, ShuihuRoomDomain> map = new HashMap<>();
    list.forEach(val -> {
      String betList = val.getBetList();
      if (EmptyUtil.stringIsNotEmpty(betList)) {
        val.setBets(JsonUtil.fromJsonToList(val.getBetList(), Integer.class));
      }
      map.put(val.getId(), val);

    });
    this.map = map;
  }

  @Override
  public ShuihuRoomDomain get(int id) {

    return map.get(id);
  }
}
